import 'package:flutter/material.dart';

class FadeAppTest extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Fade Demo',
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new MyFadeTest(title: 'Fade Demo'));
  }
}

class MyFadeTest extends StatefulWidget {
  MyFadeTest({Key key, this.title}) : super(key: key);
  final String title;

  @override
  createState() => new _MyFadeTest3();
}

class _MyFadeTest1 extends State<MyFadeTest> with TickerProviderStateMixin {
  AnimationController controller;
  CurvedAnimation curve;

  @override
  void initState() {
    controller = new AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    curve = new CurvedAnimation(parent: controller, curve: Curves.easeIn);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
          child: new Container(
              child: new FadeTransition(
                  opacity: curve,
                  child: new Text(
                    "11111111111",
                  )))),
      floatingActionButton: new FloatingActionButton(
        tooltip: 'Fade',
        child: new Icon(Icons.brush),
        onPressed: () {
          controller.forward();
        },
      ),
    );
  }
}

class _MyFadeTest2 extends State<MyFadeTest> with TickerProviderStateMixin {
  AnimationController controller;
  Animation animation;
  AnimatedWidget animatedWidget;
  AnimatedBuilder builder;
  BottomSheet sheet;

  @override
  void initState() {
    controller = new AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    animation = new Tween(begin: 0.0, end: 300.0).animate(controller)
      ..addListener(() {
        print(animation.value);
        print(controller.value);
        print("---");
      });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(),
      floatingActionButton: new FloatingActionButton(
        tooltip: 'Fade',
        child: new Icon(Icons.brush),
        onPressed: () {
          controller.forward();
        },
      ),
    );
  }
}

class _MyFadeTest3 extends State<MyFadeTest> with TickerProviderStateMixin {
  AnimationController controller;

  initState() {
    super.initState();
    print("_MyFadeTest3 init");
    controller = new AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    controller.addListener(() {
      print("controller Listener $controller.value");
    });
    controller.addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.completed) {
        controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        controller.forward();
      }
    });
    controller.forward();
  }

  void _onFloatPress() {
    if (controller.isAnimating) {
      controller.stop();
    }else{
      controller.forward();
    }
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("动画测试"),
      ),
      floatingActionButton: new FloatingActionButton(
          tooltip: 'Fade',
          child: new Icon(Icons.brush),
          onPressed: () => _onFloatPress()),
      body: _MyFadeTest3_1(listenable: CurvedAnimation(parent: controller, curve: Curves.easeIn)),
    );
  }

  dispose() {
    controller.dispose();
    super.dispose();
  }
}

class _MyFadeTest3_1 extends AnimatedWidget {
  _MyFadeTest3_1({Key key, Animation listenable})
      : super(key: key, listenable: listenable);

  static final _opacityTween = new Tween<double>(begin: 0.1, end: 1.0);
  static final _sizeTween = new Tween<double>(begin: 0.0, end: 300.0);

  Widget build(BuildContext context) {
    return new Center(
      child: new Opacity(
        opacity: _opacityTween.evaluate(super.listenable),
        child: new Container(
          decoration: BoxDecoration(border:Border.fromBorderSide(BorderSide())),
          height: _sizeTween.evaluate(super.listenable),
          width: _sizeTween.evaluate(super.listenable),
          child: new FlutterLogo(),
        ),
      ),
    );
  }
}
